/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.txt.access.capability.finder.util;

import java.util.HashMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 *
 * @author seccia
 */
public class StarResourceIDRegexCreator {

    public static final HashMap<String, String> LEAVE_NODES = new HashMap<String, String>();
    //public static final String defaultPath = "D:/Developing/ResourceIDMatch.txt";
    //The well known leaves node.
    static {
        LEAVE_NODES.put("PL1", "PL1");
        LEAVE_NODES.put("PL2", "PL2");
        LEAVE_NODES.put("PL3", "PL3");
        LEAVE_NODES.put("PL4", "PL4");
        LEAVE_NODES.put("PL5", "PL5");
        LEAVE_NODES.put("R11", "R11");
        LEAVE_NODES.put("R12", "R12");
        LEAVE_NODES.put("R13", "R13");
        LEAVE_NODES.put("R21", "R21");
        LEAVE_NODES.put("R22", "R22");
        LEAVE_NODES.put("R23", "R23");
        LEAVE_NODES.put("Cell1", "Cell1");
        LEAVE_NODES.put("Cell2", "Cell2");
        LEAVE_NODES.put("Cell3", "Cell3");
        LEAVE_NODES.put("Cell4", "Cell4");
        LEAVE_NODES.put("Cell5", "Cell5");
    }    
    public static final String REGEX_SLASH = "\\";
    public static final String SPECIAL_CHARS = "["
            + REGEX_SLASH + "("
            + REGEX_SLASH + "["
            + REGEX_SLASH + "{"
            + REGEX_SLASH + "\\"
            + REGEX_SLASH + "^"
            + REGEX_SLASH + "-"
            + REGEX_SLASH + "$"
            + REGEX_SLASH + "|"
            + REGEX_SLASH + "]"
            + REGEX_SLASH + "}"
            + REGEX_SLASH + ")"
            + REGEX_SLASH + "?"
            + REGEX_SLASH + "+"
            + REGEX_SLASH + "."
            + "]";
    //asterisco inteso come carattere
    public static final String REGEX_CHAR = "[\\w\\-\\s_\\*]";
    //asterisco inteso come nodo (DA chiarire: may contain many *)
    public static final String REGEX_CHARS = "(" + REGEX_CHAR + "+|\\*)";
    public static final String REGEX_DOT = "\\.";
    private static final String REGEX_WILD = "#";
    public static final String REGEX_APPEND = ""
            + "(" + REGEX_DOT + "(" + REGEX_CHARS + "|" + REGEX_WILD + "))*";
    public static final String REGEX_RESOURCE_ID = ""
            + "(" + REGEX_CHARS + "|" + REGEX_WILD + ")"
            + REGEX_APPEND;

    /*public static void main(String[] args) throws Exception {
        
        ArrayList<MatchTest> mts = MatchTestHelper.readMatchTestFromFile(defaultPath);
        System.out.println("***********************************");
        for (MatchTest mt : mts) {
            String topic = mt.getTopic();
            String resourceID = mt.getResourceID();
            System.out.println("Macht Test");
            System.out.println(". Resource ID: \"" + resourceID + "\"");
            System.out.println(". Topic: \"" + topic + "\"");
            String resourceIDRegex = recoverResourceIDPatternRegex(resourceID);
            System.out.println(". Regex: \"" + resourceIDRegex + "\"");
            Pattern finalPattern = Pattern.compile(resourceIDRegex, Pattern.CASE_INSENSITIVE);
            Matcher resourceIDMatcher = finalPattern.matcher(resourceID);
            if (resourceIDMatcher.matches()) {
                System.out.println(". ResourceID Match YES.");
            } else {
                System.out.println(". ResourceID Match NO.");
            }
            Matcher topicMatcher = finalPattern.matcher(topic);
            if (topicMatcher.matches()) {
                System.out.println(". Topic Match YES.");
            } else {
                System.out.println(". Topic Match NO.");
            }
            System.out.println("***********************************");
        }
    }*/

    public static String recoverResourceIDPatternRegex(String resourceID) {
        String[] items = resourceID.split("\\.");

        //The start of the regular expression
        String resourceIDRegex = "^";
        //The dots / nodes counter
        int dots = 0;
        //Useful to find any special chars within the resourceID string.
        Pattern pattern = Pattern.compile(SPECIAL_CHARS);
        //appen or not the last pattern regex section.
        boolean escape = false;
        //For each item / node in the resource ID / tree
        for (String item : items) {
            //In this case the char "*" means a node in the tree, it
            //can be replaced by the regex REGEX_CHARS.
            if (item.equals("*")) {
                resourceIDRegex += REGEX_CHARS;
                //Escape the REGEX_APPEND if the "*" is the last item.
                if (dots == items.length - 1) {
                    escape = true;
                }
            } //In this case the char "#" means one or more nodes in the tree,
            //it can be replaced by the regex REGEX_RESOURCE_ID.
            else if (item.equals("#")) {
                resourceIDRegex += REGEX_RESOURCE_ID;
                //Escape the REGEX_APPEND if the "#" is the last item.
                if (dots == items.length - 1) {
                    escape = true;
                }
            } else {
                //Find any special character in the current item.
                //replace by adding the escape char "\" in order to
                //be part of a valid regular expression.
                Matcher matcher = pattern.matcher(item);
                while (matcher.find()) {
                    String special = matcher.group();
                    item = item.replace(special, REGEX_SLASH + special);
                }
                //In this case the char "*" within the current item
                //represents any character, it can be replaced with the
                //regex REGEX_CHAR,
                item = item.replace("*", "(" + REGEX_CHAR + ")");
                resourceIDRegex += item;
            }
            //Append the char "." to the regex
            if (dots != items.length - 1) {
                resourceIDRegex += REGEX_DOT;
            }
            dots++;
        }
        //If the current item is not a "*" or a "#" then append
        //to the regex REGEX_APPEND
        if (!escape) {
            resourceIDRegex += REGEX_APPEND;
        }
        //The end of the regex.
        resourceIDRegex += "$";

        return resourceIDRegex;
    }
}
